package com.b2b.util;

import com.b2b.model.QuotationListDto;
import com.b2b.model.entity.Inquiry;
import com.b2b.model.entity.InquiryQuotation;
import com.b2b.model.entity.QuotationGoods;
import com.b2b.model.entity.QuotationRecord;
import com.b2b.model.receive.QuotationFromI;
import com.b2b.model.receive.QuotationFromQ;
import com.b2b.model.receive.QuotationInfo;
import com.b2b.model.receive.StockInfo;
import com.b2b.service.QuotationGoodsService;
import com.b2b.service.QuotationRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class QuotationUtil {
    private final QuotationGoodsUtil quotationGoodsUtil;
    private final QuotationRecordService quotationRecordService;
    private final InquiryQuotationUtil inquiryQuotationUtil;
    private final CompanyInfoUtil companyInfoUtil;
    private final InquiryUtil inquiryUtil;
    private final StockUtil stockUtil;

    @Autowired
    public QuotationUtil(QuotationGoodsUtil quotationGoodsUtil,QuotationRecordService quotationRecordService,
                         InquiryQuotationUtil inquiryQuotationUtil,CompanyInfoUtil companyInfoUtil,InquiryUtil inquiryUtil,
                         StockUtil stockUtil){
        this.quotationGoodsUtil = quotationGoodsUtil;
        this.quotationRecordService = quotationRecordService;
        this.inquiryQuotationUtil = inquiryQuotationUtil;
        this.companyInfoUtil = companyInfoUtil;
        this.inquiryUtil = inquiryUtil;
        this.stockUtil = stockUtil;
    }

    public Integer insertQuotationRecord(QuotationListDto quotationListDto){
        QuotationRecord quotationRecord = getQuotationRecord(quotationListDto);
        Integer quotationId = quotationRecordService.insert(quotationRecord);
        if (quotationId >= 0){
            quotationId = quotationRecord.getQuotationId();
            return quotationId;
        }else {
            return quotationId;
        }
    }

    public QuotationRecord selectQuotationRecord(Integer quotationId){
        QuotationRecord quotationRecord = new QuotationRecord();
        quotationRecord.setQuotationId(quotationId);
        quotationRecord = quotationRecordService.selectOne(quotationRecord);

        return quotationRecord;
    }

    public List<QuotationFromQ> selectByLaunchQId(Integer companyId){
        List<QuotationRecord> quotationRecords = selectByCompanyId(companyId);
        List<QuotationFromQ> quotationFromQS = new ArrayList<QuotationFromQ>();
        for (int i = 0; i < quotationRecords.size(); i++) {
            QuotationFromQ quotationFromQ = getQuotationFromQ(quotationRecords.get(i));
            quotationFromQS.add(quotationFromQ);
        }
        return quotationFromQS;
    }

//    public List<QuotationFromI> selectByLaunchIId(Integer companyId){
//        List<Inquiry> inquiryList = inquiryUtil.getAllByC(companyId);
//        List<QuotationFromI> quotationFromIS = new ArrayList<QuotationFromI>();
//        for (int i = 0; i < inquiryList.size(); i++) {
//            Inquiry inquiry = inquiryList.get(i);
//            List<InquiryQuotation> inquiryQuotationList = inquiryQuotationUtil.selectByInquiryId(inquiry.getInquiryId());
//            if (inquiryQuotationList.size()<=0){
//                continue;
//            }
//            List<QuotationInfo> quotationInfoList = new ArrayList<>();
//            for (int j = 0; j < inquiryQuotationList.size(); j++) {
//                Integer quotationId = inquiryQuotationList.get(j).getQuotationId();
//                Integer quotationCompanyId = getCompanyId(quotationId);
//                String quotationCompanyName = companyInfoUtil.getCompanyName(quotationCompanyId);
//
//                List<Integer> stockIdList = quotationGoodsUtil.getStockId(quotationId);
//
//                List<StockInfo> stockInfoList = new ArrayList<>();
//
//                for (int k = 0; k < stockIdList.size(); k++) {
//                    StockInfo stockInfo = stockUtil.getStockInfo(stockIdList.get(k));
//                    stockInfoList.add(stockInfo);
//                }
//                QuotationInfo quotationInfo = new QuotationInfo();
//                quotationInfo.setQuotationId(quotationId);
//                quotationInfo.setCompanyId(quotationCompanyId);
//                quotationInfo.setCompanyName(quotationCompanyName);
//                quotationInfo.setRequirementList(stockInfoList);
//                quotationInfoList.add(quotationInfo);
//            }
//            QuotationFromI quotationFromI = new QuotationFromI();
//            quotationFromI.setInquiryTitle(inquiry.getInquiryTitle());
//            quotationFromI.setInquiryId(inquiry.getInquiryId());
//            quotationFromI.setQuotationList(quotationInfoList);
//            quotationFromIS.add(quotationFromI);
//
//        }
//
//        return quotationFromIS;
//    }
    public List<QuotationFromI> selectByLaunchIId(Integer companyId){
        List<QuotationFromI> quotationFromIS = new ArrayList<QuotationFromI>();
            List<InquiryQuotation> inquiryQuotationList = inquiryQuotationUtil.getTry(companyId);
            for (int j = 0; j < inquiryQuotationList.size(); j++) {
                QuotationFromI quotationFromI = new QuotationFromI();
                Integer quotationId = inquiryQuotationList.get(j).getQuotationId();
                Integer inquiryId = inquiryQuotationList.get(j).getInquiryId();
                Integer quotationCompanyId = getCompanyId(quotationId);
                String quotationCompanyName = companyInfoUtil.getCompanyName(quotationCompanyId);

                List<Integer> stockIdList = quotationGoodsUtil.getStockId(quotationId);

                List<StockInfo> stockInfoList = new ArrayList<>();

                for (int k = 0; k < stockIdList.size(); k++) {
                    StockInfo stockInfo = stockUtil.getStockInfo(stockIdList.get(k));
                    stockInfoList.add(stockInfo);
                }
                quotationFromI.setQuotationId(quotationId);
                quotationFromI.setInquiryId(inquiryId);
                quotationFromI.setInquiryTitle(inquiryUtil.getInquiryTitle(inquiryId));
                quotationFromI.setCompanyId(quotationCompanyId);
                quotationFromI.setCompanyName(quotationCompanyName);
                quotationFromI.setRequirementList(stockInfoList);

                quotationFromIS.add(quotationFromI);
            }
        return quotationFromIS;
    }
    public List<InquiryQuotation> selectByLaunchITry(Integer companyId){
            List<InquiryQuotation> inquiryQuotationList = inquiryQuotationUtil.getTry(companyId);
            return inquiryQuotationList;

    }
//    public List<QuotationFromI> selectByInquiryIdFromA(Integer inquiryId){
//        List<QuotationFromI> quotationFromIS = new ArrayList<>();
//        List<InquiryQuotation> inquiryQuotationList = inquiryQuotationUtil.selectByInquiryId(inquiryId);
//        for (int j = 0; j < inquiryQuotationList.size(); j++) {
//            QuotationFromI quotationFromI = new QuotationFromI();
//            Integer quotationId = inquiryQuotationList.get(j).getQuotationId();
//            quotationFromI.setQuotationId(quotationId);
//            Integer quotationCompanyId = getCompanyId(quotationId);
//            String quotationCompanyName = companyInfoUtil.getCompanyName(quotationCompanyId);
//
//            List<Integer> stockIdList = quotationGoodsUtil.getStockId(quotationId);
//
//            List<StockInfo> stockInfoList = new ArrayList<>();
//
//            for (int k = 0; k < stockIdList.size(); k++) {
//                StockInfo stockInfo = stockUtil.getStockInfo(stockIdList.get(k));
//                stockInfoList.add(stockInfo);
//            }
//
//            QuotationInfo quotationInfo = new QuotationInfo();
//            quotationInfo.setQuotationId(quotationId);
//            quotationInfo.setCompanyId(quotationCompanyId);
//            quotationInfo.setCompanyName(quotationCompanyName);
//            quotationInfo.setRequirementList(stockInfoList);
//            quotationInfoList.add(quotationInfo);
//        }
//        return quotationFromIS;
//    }
    public List<QuotationRecord> selectByCompanyId(Integer companyId){
        QuotationRecord quotationRecord = new QuotationRecord();
        quotationRecord.setCompanyId(companyId);
        List<QuotationRecord> quotationRecords = quotationRecordService.select(quotationRecord);
        return quotationRecords;
    }

    public List<QuotationRecord> selectByInquiryId(Integer inquiryId){
        List<InquiryQuotation> inquiryQuotationList = inquiryQuotationUtil.selectByInquiryId(inquiryId);
        List<QuotationRecord> quotationRecords = new ArrayList<QuotationRecord>();
        for (int i = 0; i < inquiryQuotationList.size(); i++) {
            Integer quotationId = inquiryQuotationList.get(i).getQuotationId();
            quotationRecords.add(selectQuotationRecord(quotationId));
        }
        return quotationRecords;
    }
    public Integer deleteQuotation(Integer quotationId){
        QuotationRecord quotationRecord = new QuotationRecord();
        quotationRecord.setQuotationId(quotationId);

        Integer deleteGoods = quotationGoodsUtil.deleteGoods(quotationId);
        Integer deleteInquiryQuotation = inquiryQuotationUtil.deleteInquiryQuotation(quotationId);
        if (deleteGoods<=0){
            return deleteGoods;
        }else if (deleteInquiryQuotation<=0){
            return deleteInquiryQuotation;
        }else {
            Integer deleteQuotation = quotationRecordService.delete(quotationRecord);
            return deleteGoods;
        }
    }

    public Integer deleteForInquiry(Integer inquiryId){
        List<InquiryQuotation> inquiryQuotationList = inquiryQuotationUtil.selectByInquiryId(inquiryId);
        Integer count = -1;
        for (int i = 0; i < inquiryQuotationList.size(); i++) {
            count = deleteQuotation(inquiryQuotationList.get(i).getQuotationId());
        }
        return count;
    }

    public QuotationFromQ getQuotationFromQ(QuotationRecord quotation){
        QuotationFromQ quotationFromQ = new QuotationFromQ();
        Integer quotationId = quotation.getQuotationId();
        List<Integer> stockIdList = quotationGoodsUtil.getStockId(quotationId);

        List<StockInfo> stockInfoList = new ArrayList<>();

        for (int k = 0; k < stockIdList.size(); k++) {
            StockInfo stockInfo = stockUtil.getStockInfo(stockIdList.get(k));
            stockInfoList.add(stockInfo);
        }

        quotationFromQ.setQuotationId(quotationId);
        Inquiry inquiry = inquiryQuotationUtil.getInquiryByQuotationId(quotationId);
        quotationFromQ.setInquiryId(inquiry.getInquiryId());
        quotationFromQ.setPostscript(quotation.getPostscript());
        quotationFromQ.setInquiryTitle(inquiry.getInquiryTitle());
        quotationFromQ.setCompanyId(inquiry.getCompanyId());
        quotationFromQ.setCompanyName(companyInfoUtil.getCompanyName(inquiry.getCompanyId()));
        quotationFromQ.setRequirementList(stockInfoList);
        return quotationFromQ;
    }


    public QuotationRecord getQuotationRecord(QuotationListDto quotationListDto){
        QuotationRecord quotationRecord = new QuotationRecord();

        quotationRecord.setQuotationId(quotationListDto.getQuotationId());
        quotationRecord.setQuotationTime(new Date());
        quotationRecord.setCompanyId(quotationListDto.getCompanyId());
        quotationRecord.setPostscript(quotationListDto.getPostscript());

        return quotationRecord;
    }

    public Integer getCompanyId(Integer quotationId){
        QuotationRecord quotationRecord = new QuotationRecord();
        quotationRecord.setQuotationId(quotationId);
        quotationRecord = quotationRecordService.selectByPrimaryKey(quotationRecord);
        Integer companyId = quotationRecord.getCompanyId();

        return companyId;
    }
    public Integer updateQuotation(QuotationRecord quotationRecord){
        Integer count = quotationRecordService.updateByPrimaryKey(quotationRecord);

        return count;
    }
}
